package leetcode_tag.树;

import leetcode.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class LeetCode101 {

    public boolean isSymmetric(TreeNode root) {
        if (root == null)
            return true;
        return helper(root.left, root.right);
    }

    //递归检查
    private boolean helper(TreeNode left, TreeNode right) {
        if (left == null && right == null)
            return true;
        else if (left != null && right != null) {
            if (left.val == right.val)
                return helper(left.left, right.right) && helper(left.right, right.left);
        } else
            return false;
        return false;
    }
    //层次遍历检查
    // 构造两个节点 按照不同的方向前进
    public boolean check(TreeNode u, TreeNode v) {
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        q.offer(u);
        q.offer(v);
        while (!q.isEmpty()) {
            u = q.poll();
            v = q.poll();
            if (u == null && v == null) {
                continue;
            }
            if ((u == null || v == null) || (u.val != v.val)) {
                return false;
            }

            q.offer(u.left);
            q.offer(v.right);

            q.offer(u.right);
            q.offer(v.left);
        }
        return true;
    }
}
